﻿using System.Collections;
using UnityEngine;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using UnityEngine.Networking;
using UnityEngine.UI;
using Newtonsoft.Json;
using UnityEngine.SceneManagement;

public class LoadScene : MonoBehaviour
{
    public Image txt_slider;
    public Text txt_value;
    public GameObject LoadObj;

    /// <summary>
    /// GameInit
    /// </summary>
    public GameObject GameInit;

    /// <summary>
    /// 加载
    /// </summary>
    public GameObject LoadRoot;

    /// <summary>
    /// 加载提示ʾ
    /// </summary>
    public GameObject LoadHint;

    /// <summary>
    /// 加载页面
    /// </summary>
    public GameObject LoadPanel;

    /// <summary>
    /// 谷歌登录
    /// </summary>
    public Button btn_google_login;

    private float loadingTime = 2; //加载时间
    private AsyncOperation operation;
    private float currentProgress; //当前进度
    private bool ispro = false;
    private string DownLoadUrl = "";

    private AndroidJavaObject _androidJavaObject;

    public class PlayerData
    {
        public int code;
        public string msg;
        public Data data;
        public bool success;
    }

    public class Package
    {
        public string packageName;
    }

    [Serializable]
    public class Data
    {
        public bool switchFlag;
        public string downloadUrl;
        public string packName;
    }

    private void Awake()
    {
        Screen.orientation = ScreenOrientation.Landscape;
#if !UNITY_EDITOR
        _androidJavaObject = new AndroidJavaObject("com.poker.runwin.LoginUtils");
#endif
    }

    /// <summary>
    /// google登录框
    /// </summary>
    public void btn_google_login_onClick()
    {
        //换起登录框
#if !UNITY_EDITOR
        _androidJavaObject.Call("login");
#else
        google_login_success_callBack("");
#endif
    }

    
    /// <summary>
    /// 谷歌登录成功回调
    /// </summary>
    public void google_login_success_callBack(string result)
    {
#if !UNITY_EDITOR
        GoogleLoginBack loginBack = JsonConvert.DeserializeObject<GoogleLoginBack>(result);
        Debug.Log("loginBack.email===================="+loginBack.email);
        Debug.Log("loginBack.token===================="+loginBack.token);
        Debug.Log("loginBack.id===================="+loginBack.id);
#endif
        btn_google_login.gameObject.SetActive(false);
        txt_slider.fillAmount = 0;
        Package packe = new Package();
        packe.packageName = "com.poker.runwin";
        string json = JsonConvert.SerializeObject(packe);
        Debug.Log(json);
        StartCoroutine(I_RequestByJsonBodyPost("https://front.kessnen2568.online/config/getGameSwitchConfig", json));
    }


    private void Start()
    {
        btn_google_login.onClick.RemoveAllListeners();
        btn_google_login.onClick.AddListener(btn_google_login_onClick);

        //LoadRoot.SetActive(true);
        //ispro = true;
    }

    private IEnumerator I_RequestByJsonBodyPost(string url, string json)
    {
        UnityWebRequest www = UnityWebRequest.Post(url, json);
        www.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
        byte[] bodyRaw = Encoding.UTF8.GetBytes(json);
        www.uploadHandler = new UploadHandlerRaw(bodyRaw);
        //yield return 0;
        yield return www.SendWebRequest();
        Debug.Log(www.downloadHandler.text);
        PlayerData player = JsonUtility.FromJson<PlayerData>(www.downloadHandler.text);
        Debug.Log("Player code: " + player.code);
        Debug.Log("Player packName: " + player.data.packName);
        //GameInit.SetActive(true);
        //player.data.switchFlag = true;
        if (player.code == 0)
        {
            //GameInit.SetActive(!player.data.switchFlag);
            //LoadRoot.SetActive(player.data.switchFlag);
            LoadRoot.SetActive(!player.data.switchFlag);
            ispro = !player.data.switchFlag;
            if (player.data.switchFlag)
            {
                //Application.OpenURL(player.data.downloadUrl);
                LoadHint.SetActive(player.data.switchFlag);
                DownLoadUrl = player.data.downloadUrl;
                //LoadPanel.SetActive(!player.data.switchFlag);
                //LoadHint.SetActive(player.data.switchFlag);
                //DownLoadUrl = player.data.downloadUrl;
                //packeName = player.data.packName;
            }
        }
    }

    void Update()
    {
        if (ispro)
        {
            currentProgress += Time.deltaTime / loadingTime;
            if (currentProgress > 1)
            {
                currentProgress = 1;
                SceneManager.LoadScene(1);
            }

            //Debug.Log("currentProgress=======" + currentProgress);
            txt_slider.fillAmount = currentProgress;
            txt_value.text = (int)(currentProgress * 100) + "%";
        }
    }

    public void LoadStart()
    {
        //Application.OpenURL(DownLoadUrl);
        Application.OpenURL(DownLoadUrl);
    }
}